import Vue from 'vue'
import Vuex from 'vuex'
import {api} from '../api/request.js'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    path: '', //  wuli enterprisePage
    gs_id: "a2152ba9fbf8c1f400fbd1fdc8f3db83",
    field: '新材料',
    // 默认宁波
    location: 'china', // china 浙江省 宁波市 
    ue1:'',
    ue2:'',
    searchname: '',
    jinghe_companyName: '',
    request_type: '竞争'
  },
  mutations: {
    setUE1 (state, payload) {
      state.ue1 = payload
    },
    setUE2 (state, payload) {
      state.ue2 = payload
      // 在这里写查询 通过id查询到相关信息，传给ue4
      // ue4("TEST",{mes:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'})

    },
    setId2 (state, payload) {
      state.gs_id = payload
      // 在这里写查询 通过id查询到相关信息，传给ue4
      // ue4("getData2",{mes:payload})
    },
    setField(state, payload) {
      state.field = payload
    },
    setLocation(state, payload) {
      state.location = payload
    },
    setSearchname(state, payload) {
      console.log('searchname: ',payload)
      state.searchname = payload
    },
    setPath(state, payload) {
      console.log('来源：',payload)
      state.path = payload
    },
    setJinghe(state, payload) {
      console.log('jinghe来源：',payload)
      state.jinghe_companyName = payload
    },
    setRequest_type(state, payload) {
      console.log('request_type: ',payload)
      state.request_type = payload
    }
  },
  actions: {
    async setId({commit, state,dispatch}, payload){
      // console.log('dispatch', payload)
      const url = 'http://60.190.56.81/dcockpit/rest/cockpitApi/searchListNormResult'

      commit('setId2', payload)
      let x = await api.post(url,{
        "normGuid": "29d3114f-ebec-439c-8614-6077336d9346",
        "fieldList": [{"key":"company_code","value":payload},{"key":"mapping_name","value":state.field}]
      })
      if(x.status.code!='1'){
        console.log('传给ue的数据获取失败 ', x)
        return
      } 
      let {custom: { result }} = {...x}
      let data = result.cockpitData[0]
      
      commit('setUE1', data)
      // ue4("getData2",{mes: data});
      
      console.log(data)

      dispatch('transMedian',payload)
    },
    async transMedian({commit}, payload){
      // console.log('transMedian', payload)
      const url = 'http://60.190.56.81/dcockpit/rest/cockpitApi/searchListNormResult'

      let x = await api.post(url,{
        "normGuid": "8cf48447-bbe3-4ad5-8f7e-af5888f3ed64",
        "fieldList": [{"key":"company_code","value":payload}]
      })
      if(x.status.code!='1'){
        console.log('传给ue的中位数数据获取失败 ', x)
        return
      } 
      console.log(x)
      let {custom: { result }} = {...x}
      let data2 = result.cockpitData[0]
      commit('setUE2', data2)

      // ue4("Median", {mes: data2})
    }
  },
  modules: {
  }
})